package com.xh.bussiness.xkw.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.xh.bussiness.system.bean.request.DictionaryRequest;
import com.xh.bussiness.system.model.Dictionary;
import com.xh.bussiness.system.model.DictionaryRel;
import com.xh.bussiness.system.service.IDictionaryRelService;
import com.xh.bussiness.system.service.IDictionaryService;
import com.xh.bussiness.xkw.constant.XkwAPIURL;
import com.xh.bussiness.xkw.dao.XkwSubjectMapper;
import com.xh.bussiness.xkw.model.XkwSubject;
import com.xh.bussiness.xkw.service.IXkwSubjectService;
import com.xh.bussiness.xkw.utils.ConfigUtils;
import com.xh.bussiness.xkw.utils.XkwHttpsUtils;
import com.xh.core.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class XkwSubjectServiceImpl extends BaseServiceImpl<XkwSubjectMapper, XkwSubject> implements IXkwSubjectService {

    private final String XkwApiSeriveUrl;

    @Autowired
    public XkwSubjectServiceImpl(ConfigUtils configUtils) {
        XkwApiSeriveUrl = configUtils.getXkwApiSeriveUrl();
    }

    @Autowired
    private ConfigUtils configUtils;
    @Autowired
    private IDictionaryService dictionaryService;
    @Autowired
    private IDictionaryRelService dictionaryRelService;

    /**
     * 查询所有学科网科目
     *
     * @throws Exception
     */
    public List<XkwSubject> SysXkwSubject() throws Exception {
        List<XkwSubject> xkwSubjectList = new ArrayList<>();
        String xkwApiSeriveUrl = configUtils.getXkwApiSeriveUrl();
        System.out.println("xkwApiSeriveUrl" + xkwApiSeriveUrl);

        String url = XkwApiSeriveUrl + XkwAPIURL.GET_BANKS;
        String subjectJson = XkwHttpsUtils.doGet(url);
        List<Map> parse = (List<Map>) JSONArray.parse(subjectJson);
        parse.forEach(map -> {
            Integer id = Integer.valueOf(map.get("id").toString());
            String name = map.get("name").toString();
            Integer stageId = Integer.valueOf(map.get("stageId").toString());
            Dictionary subject = new Dictionary();
            subject.setId("subject_" + String.valueOf(id));
            subject.setName(name);
            subject.setCode(String.valueOf(id));
            subject.setOrderNo(id);
            subject.setTypeCode("SUBJECT");
            dictionaryService.insert(subject);
            DictionaryRel subjectRel = new DictionaryRel();
            if (stageId == 1) {
                subjectRel.setParentCode("primary");
            }
            if (stageId == 2) {
                subjectRel.setParentCode("middle");
            }
            if (stageId == 3) {
                subjectRel.setParentCode("high");
            }
            subjectRel.setChildCode(String.valueOf(id));
            subjectRel.setChildTypeCode("SUBJECT");
            dictionaryRelService.insert(subjectRel);
        });
        return xkwSubjectList;
    }

    @Override
    public List<Dictionary> SysXkwGrade() {
        String url = XkwApiSeriveUrl + XkwAPIURL.GET_GRADES;
        String subjectJson = XkwHttpsUtils.doGet(url);
        List<Map> parse = (List<Map>) JSONArray.parse(subjectJson);
        parse.forEach(map -> {
            Integer id = Integer.valueOf(map.get("id").toString());
            String name = map.get("name").toString();
            Integer stageId = Integer.valueOf(map.get("stageId").toString());
            Dictionary grade = new Dictionary();
            grade.setName(name);
            grade.setId("grade_" + String.valueOf(id));
            grade.setCode(String.valueOf(id));
            grade.setOrderNo(id);
            grade.setTypeCode("GRADE");
            dictionaryService.insert(grade);
            DictionaryRel gradeRel = new DictionaryRel();
            if (stageId == 1) {
                gradeRel.setParentCode("primary");
            }
            if (stageId == 2) {
                gradeRel.setParentCode("middle");
            }
            if (stageId == 3) {
                gradeRel.setParentCode("high");
            }
            gradeRel.setChildCode(String.valueOf(id));
            gradeRel.setChildTypeCode("GRADE");
            dictionaryRelService.insert(gradeRel);
        });
        return null;
    }

    @Override
    public List<Dictionary> sysQuestionSource() {
        DictionaryRequest bean = new DictionaryRequest();
        bean.setTypeCode("SUBJECT");
        List<Dictionary> subjects = dictionaryService.queryDictionary(bean);
        for (Dictionary subject : subjects) {
            String url = XkwApiSeriveUrl + XkwAPIURL.QUERY_BANKS_PAPER_TYPES;
            url = String.format(url, subject.getCode());
            String subjectJson = XkwHttpsUtils.doGet(url);
            List<Map> parse = (List<Map>) JSONArray.parse(subjectJson);
            parse.forEach(map -> {
                Integer id = Integer.valueOf(map.get("id").toString());
                String name = map.get("name").toString();
                Dictionary grade = new Dictionary();
                grade.setName(name);
                grade.setId("questSource_" + String.valueOf(id));
                grade.setCode(String.valueOf(id));
                grade.setOrderNo(id);
                grade.setTypeCode("QUES_SOURCE");
                dictionaryService.insert(grade);
            });
        }
        return null;
    }

    @Override
    public List<Dictionary> sysQuestionDifficult() {
        String url = XkwApiSeriveUrl + XkwAPIURL.QUERY_BANKS_PAPER_TYPES;
        String subjectJson = XkwHttpsUtils.doGet(url);
        List<Map> parse = (List<Map>) JSONArray.parse(subjectJson);
        parse.forEach(map -> {
            Integer id = Integer.valueOf(map.get("id").toString());
            String name = map.get("name").toString();
            Dictionary source = new Dictionary();
            source.setId("quesDiff" + String.valueOf(id));
            source.setName(name);
            source.setCode(String.valueOf(id));
            source.setOrderNo(id);
            source.setTypeCode("QUES_DIFF");
            dictionaryService.insert(source);
        });
        return null;
    }

}
